package com.hd.sup.modules.vehicle.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.hd.common.constant.BizConstant;
import com.hd.common.utils.ValueUtils;
import com.hd.sup.modules.vehicle.mapper.VehicleDailyStatisMapper;
import com.hd.sup.modules.vehicle.model.VehicleDailyStatis;
import com.hd.sup.modules.vehicle.service.IVehicleDailyStatisService;
import com.hd.sup.modules.vehicle.service.IVehicleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author chentl123
 * @since 2020-12-28
 */
@Service
public class VehicleDailyStatisServiceImpl extends ServiceImpl<VehicleDailyStatisMapper, VehicleDailyStatis> implements IVehicleDailyStatisService {


    @Resource
    private IVehicleService vehicleService;

    @Override
    public List<VehicleDailyStatis> getDailyVehicleNumData(VehicleDailyStatis param, List<String> deptIds) {
        return this.baseMapper.getDailyVehicleNumData(param, deptIds);
    }

    @Override
    public VehicleDailyStatis getDailyVehicleSumData(String currentDateStr, List<String> deptIds) {
        return this.baseMapper.getDailyVehicleSumData(currentDateStr, deptIds);
    }


    @Override
    public List<Map<String, Object>> getVehicleTop5(List<String> deptIds) {
        return this.baseMapper.getVehicleTop5(deptIds);
    }

    @Override
    public void fillVehicleByTransTypeForEmpty(Map<String, Object> resultMap, List<String> deptIds) {
        for (BizConstant.VehicleTransType one : BizConstant.VehicleTransType.values()) {
            Map<String, Object> v = Maps.newHashMap();
            Integer vehicleSum = vehicleService.countVehicleNum(null, one.key(), deptIds);
            v.put("online_num", 0);
            v.put("alarm_num", 0);
            v.put("go_online_sum", 0);
            v.put("vehicle_sum", vehicleSum);
            v.put("online_rate", 0);
            resultMap.put(one.property(), v);
        }
    }

    @Override
    public void fillVehicleByTransType(Map<String, Object> resultMap, List<VehicleDailyStatis> list, List<String> deptIds) {
        for (BizConstant.VehicleTransType one : BizConstant.VehicleTransType.values()) {
            Map<String, Object> v = Maps.newHashMap();
            Integer vehicleSum = vehicleService.countVehicleNum(null, one.key(), deptIds);
            AtomicBoolean contains = new AtomicBoolean(false);
            AtomicReference<VehicleDailyStatis> current = new AtomicReference<>();

            list.forEach(vehicleStatisData -> {
                String transType = vehicleStatisData.getTransType();
                if (one.key().equals(transType)) {
                    contains.set(true);
                    current.set(vehicleStatisData);
                }
            });


            if (contains.get()) {
                //包含
                Integer onlineNum = ValueUtils.parseInteger(current.get().getOnlineSum() == null ? 0 : current.get().getOnlineSum());
                Integer alarmNum = ValueUtils.parseInteger(current.get().getAlarmVehicleNum() == null ? 0 : current.get().getAlarmVehicleNum());
                Integer goOnlineSum = ValueUtils.parseInteger(current.get().getGoOnlineSum() == null ? 0 : current.get().getGoOnlineSum());
                String onlineRate = ValueUtils.getRate(onlineNum, vehicleSum, 2);

                v.put("online_num", onlineNum);
                v.put("alarm_num", alarmNum);
                v.put("go_online_sum", goOnlineSum);
                v.put("vehicle_sum", vehicleSum);
                v.put("online_rate", onlineRate);
                resultMap.put(one.property(), v);
            } else {
                //不包含
                v.put("online_num", 0);
                v.put("alarm_num", 0);
                v.put("go_online_sum", 0);
                v.put("vehicle_sum", vehicleSum);
                v.put("online_rate", 0);
                resultMap.put(one.property(), v);
            }


        }
    }

    @Override
    public List<Map<String, Object>> getGoOnlineTrendData(String dateRange, String startDateStr, String endDateStr, String transType, List<String> deptIds) {
        return this.baseMapper.getGoOnlineTrendData(dateRange, startDateStr, endDateStr, transType, deptIds);
    }

    @Override
    public List<VehicleDailyStatis> getTodayStatisData(String currentDate) {
        return this.baseMapper.getTodayStatisData(currentDate);
    }

    @Override
    public void refreshVehicleDailyStatis(VehicleDailyStatis vehicleDailyStatis) {
        this.baseMapper.refreshVehicleDailyStatis(vehicleDailyStatis);
    }
}
